<template>
  <div class="app-container">
    <el-form ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="切换年级">
        <el-select v-model="value" placeholder="请选择年级"  @change="getPointClassesList(value)">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:classmanagetest:add']"
        >新增班级</el-button>
      </el-col>
      <!-- <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            type="success"-->
      <!--            plain-->
      <!--            icon="el-icon-edit"-->
      <!--            size="mini"-->
      <!--            :disabled="single"-->
      <!--            @click="handleUpdate"-->
      <!--            v-hasPermi="['system:classmanagetest:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
       <el-col :span="1.5">
              <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDeleteList"
                  v-hasPermi="['system:classmanagetest:remove']"
              >删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classmanagetestList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="班级编号" align="center" prop="gradeId" />
      <el-table-column label="班主任" align="center" prop="headTeacherName" />
      <el-table-column label="班级人数" align="center" prop="countStudents" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:classmanagetest:edit']"
          >查看</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:classmanagetest:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="addForm" :model="addForm" :rules="rules" label-width="100px">
        <el-form-item label="年级" prop="year">
          <el-input v-model="addForm.grade" placeholder="新增班级的年级" />
        </el-form-item>
        <el-form-item label="班级数" prop="headTeacherName">
          <el-input v-model="addForm.count" placeholder="需要创建的班级数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addClassmanage">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { listClassmanagetest, classRemoveAPI,classAddAPI, getClassgradeAPI,classRemoveListAPI} from "@/api/system/classmanagetest";

export default {
  name: "Classmanagetest",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      classmanagetestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        yearId: 2021,
        pageNum: 1,
        pageSize: 10,
        gradeId: null,
        headTeacherName: null,
        countStudents: null
      },
      // 表单参数
      form: {},
      //新增表单
      addForm: {},
      // 表单校验
      rules: {},
      options: [],
      value: ''
    };
  },
  mounted(){
    this.value=this.queryParams.yearId+'年级'
  },
  created() {
    this.getList();
    this.getClassGrade();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listClassmanagetest(this.queryParams).then(response => {
        this.classmanagetestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询【请填写功能名称】列表 */
    getPointClassesList(yearId) {
      // console.log(yearId)
      this.loading = true;
      listClassmanagetest({yearId}).then(response => {
        this.classmanagetestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gradeId: null,
        headTeacherName: null,
        countStudents: null
      };
      this.resetForm("form");
    },
    myAddreset() {
      this.addForm = {
        grade: null,
        count: null,
      };
      this.resetForm("addForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gradeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.myAddreset();
      this.open = true;
      this.title = "新增班级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gradeId = row.gradeId || this.ids
      // getClassmanagetest(gradeId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改【请填写功能名称】";
      // });
      this.$router.push({name:'studentInfo',params:{gradeId:gradeId}})
    },
    addClassmanage() {
      this.$refs["addForm"].validate(valid => {
        if (valid) {
          classAddAPI(this.addForm).then(res => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
            this.getClassGrade();
          })
        }
      });
    },
    /*查询年份*/
    getClassGrade(){
      getClassgradeAPI({}).then(res=>{
        let res1 = res.rows
        this.options = res1.map(key=>{
          let obj = {
            value:key,
            label:key,
          }
          return obj;
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gradeId = row.gradeId ;
      console.log(gradeId)
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + gradeId + '"的数据项？').then(function () {
        return classRemoveAPI({gradeId});
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
        this.getClassGrade();
      }).catch(() => {
      });
    },
    /** 批量删除按钮操作 */
    handleDeleteList() {
      const gradeIds = this.ids;
      console.log(gradeIds)
      this.$modal.confirm('确认是否删除？').then(function () {
        return classRemoveListAPI(gradeIds);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
        this.getClassGrade();
      }).catch(() => {
      });
    },
  }
}

</script>
<style>
.el-dropdown {
  vertical-align: top;
}
.el-dropdown + .el-dropdown {
  margin-left: 15px;
}
.el-dropdown {
  float: left;
}
.el-icon-arrow-down {
  font-size: 12px;

}
</style>